//
// Created by ta&to on 2022/8/23.
//

#ifndef HTTPSERVER_HTTP_LOG_H
#define HTTPSERVER_HTTP_LOG_H

#include <sys/types.h>
#include <stdarg.h>
#include <string.h>
#include "dts.h"
#include "tree.h"
#include "http_thread.h"
#include "http_alloc.h"
#include "http_string.h"
#include "http_time.h"
#include "http_file.h"
typedef struct log_s log_t;
typedef char *(*http_log_handler_pt) (log_t *logs, char *buf, size_t len);

#define LOG_CRITICAL     1
#define LOG_ERROR       2
#define LOG_WARNING     3
#define LOG_DEBUG       4
#define LOG_INFO        5

#define MAX_ERROR_STR 2048
typedef struct log_task_s log_task_t;
struct log_task_s{
    int err;
    dts data;
    struct file_s *file;
};

struct log_s {
    int level;
    struct Queue task_queue;
    struct file_s *debug_file;
    struct file_s *err_file;
    http_log_handler_pt   handler;
};

void log_init(log_t *logs);
void log_write_core(int level, int err, log_t *logs, char *fmt, ...);
void http_log_cycle(void *args, thread_task_args_t *args_t);

#define debug(logs, fmt, ...)\
        log_write_core(LOG_INFO, 0, logs, fmt,  ##__VA_ARGS__)
#define info(logs, fmt, ...) \
        log_write_core(LOG_DEBUG, 0, logs, fmt,  ##__VA_ARGS__)
#define warn(logs, fmt, ...)\
        log_write_core(LOG_WARNING, 0, logs, fmt,  ##__VA_ARGS__)
#define error(logs, fmt, ...)\
        log_write_core(LOG_ERROR, 1, logs, fmt,  ##__VA_ARGS__)
#define critical(logs, fmt, ...)\
        log_write_core(LOG_CRITICAL, 1, logs, fmt,  ##__VA_ARGS__)
#endif //HTTPSERVER_HTTP_LOG_H
